﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Drawing;
using System.Windows.Forms;

namespace APC
{
    public class LinkCellElement : DetailListViewDataCellElement
    {
        #region Declarations

        public delegate void Item_Selected(object o, EventArgs e);
        public event Item_Selected OnItemSelected;

        private bool ShowLink = false;
        private string LocationName = "";

        APC.RawMaterials rm;
        ListViewDetailColumn cl;

        #endregion

        public LinkCellElement(DetailListViewVisualItem item, ListViewDetailColumn column)
            : base(item, column)
        {
            cl = column;

            if (item.Data.Tag is APC.RawMaterials)
            {
                rm = (APC.RawMaterials)item.Data.Tag;
                ShowLink = false;

                if (rm.ItemType != RawMaterials.eItemType.TypeSum)
                {
                    if (rm.LocationName == "Select Location")
                    {
                        ShowLink = true;
                        LocationName = rm.LocationName;
                    }
                    else
                    {
                        LocationName = rm.LocationName;
                        ShowLink = false;
                    }
                }
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);

            if (this.rm.LocationName == "Select Location") //TODO: 'Select Location' must be changed to a non-cultural flag since text can be in other language
            {
                frmPickList frm = new frmPickList(rm.Id, frmPickList.SelectType.TypeSingleSelect, frmPickList.ObjectType.TypeLocation);
                frm.OnItemSelected += new frmPickList.Item_Selected(Location_Selected);
                frm.ShowDialog();
            }
        }

        public override void Synchronize()
        {
            base.Synchronize();

            if (ShowLink) // "Select Location"
            {
                this.Image = APC.Properties.Resources.alert;
                this.ImageAlignment = ContentAlignment.MiddleLeft;
                this.Text = "Select Location";
                this.TextAlignment = ContentAlignment.MiddleCenter;
                this.Font = new Font(this.Font, FontStyle.Underline);
                this.ForeColor = Color.DarkBlue;
            }
            else
            {
                this.Text = LocationName;
            }
        }

        protected override Type ThemeEffectiveType
        {
            get
            {
                return typeof(DetailListViewDataCellElement);
            }
        }

        private void Location_Selected(object sender, EventArgs e)
        {
            APC.Common.Location l = (APC.Common.Location)sender;

            //Return RawMaterial object with inserted TagListGroupId and LocationPosition
            rm.TagListeGroupId = Convert.ToInt32(l.TagListeGroupId);
            rm.LocationPosition = Convert.ToInt32(l.LocationPosition);
            rm.LocationId = l.Id;

            OnItemSelected(rm, e);
        }

    }
}
